package com.sanctuary.dao.impls;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sanctuary.dao.interfaces.CommentDao;
import com.sanctuary.models.Comment;

public class CommentDaoImpl extends SqlMapClientDaoSupport implements
		CommentDao {

	public int delete(Comment c) throws SQLException {
		return getSqlMapClientTemplate().delete("delete-comment",c);
	}

	public List<Comment> findByObjectAndOwner(int oid, int owner)
			throws SQLException 
	{
		Comment c = new Comment();
		c.setObjectId(oid);
		c.setOwnerId(owner);
		return (List<Comment>) getSqlMapClientTemplate().queryForList("findByObjectOwner-comment",c);
	}

	public List<Comment> findByUser(int uid) throws SQLException {
		Comment c = new Comment();
		c.setUid(uid);
		return (List<Comment>) getSqlMapClientTemplate().queryForList("findByUser-comment",c);
	}

	public int save(Comment c) throws SQLException {
		if (c.getId() == 0) {
			// new comment, insert
			return ((Comment) getSqlMapClientTemplate().insert("insert-comment",c)).getId();
		} else {
			return getSqlMapClientTemplate().update("update-comment",c);
		}
	}
}
